Packing Paradox and Dbase tables


When you delete records from Paradox or DBase tables, the size of phisycal file
will not reduced by this deletion because it is a logical deletion. When you add
new records to that tables, the new records will occupy the same location of deleted
records, if all spaces that left by deleted records already occupied, then new records
will allocate new size for the table. You can remove unused space allocated by deleted
records by packing the table. Packing Paradox and DBase tables does not remove only
deleted records, but it regenerate out-of-date indexes, also it removes passwords
from tables. To pack Paradox or DBase table call this function:


Add to uses clause:

DB, DBTables, and BDE

procedure PackTable(Table: TTable);
var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;

begin
 // Make sure the table is open exclusively so we can get the db handle...
 if Table.Active = False then
  raise EDatabaseError.Create('Table must be opened to pack');
if Table.Exclusive = False then
  raise EDatabaseError.Create('Table must be opened exclusively to pack');

 // Get the table properties to determine table type...
 Check(DbiGetCursorProps(Table.Handle, Props));

 // If the table is a Paradox table, you must call DbiDoRestructure...
 if Props.szTableType = szPARADOX then
begin
   // Blank out the structure...
   FillChar(TableDesc, sizeof(TableDesc), 0);
   //  Get the database handle from the table's cursor handle...
   Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
   // Put the table name in the table descriptor...
   StrPCopy(TableDesc.szTblName, Table.TableName);
   // Put the table type in the table descriptor...
   StrPCopy(TableDesc.szTblType, Props.szTableType);
   // Set the Pack option in the table descriptor to TRUE...
   TableDesc.bPack := True;
   // Close the table so the restructure can complete...
   Table.Close;
   // Call DbiDoRestructure...
   Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));
end
else
   // If the table is a dBASE table, simply call DbiPackTable...
   if Props.szTableType = szDBASE then
    Check(DbiPackTable(Table.DBHandle, Table.Handle, nil, szDBASE, TRUE))
  else
     // Pack only works on PAradox or dBASE; nothing else...
     raise EDatabaseError.Create('Table must be either of Paradox or dBASE ' +
             'type to pack');

Table.Open;

end;


Example of calling this function:


 Table1.TableName:= 'd:\phone\phone.db';
Table1.Exclusive:= True;
Table1.Open;
PackTable(Table1);
Table1.Close;

Source:
Borland documentation
www.borland.com